#! /usr/bin/python
# -*- coding: utf-8 -*-

from google.appengine.ext import db
import validator

#Model for ADvertisement-Ebook

titleMaxLen = 256
authorMaxLen = 32
descrMaxLen = 480
linkMaxLen = 400
imgsrcMaxLen = 400


#
#source: 0--当当网；1--北发图书网； 2--China-Pub；
#        3--卓越亚马逊；4--新华书店.com
#        5--99网上书城
#
sourceInfo = [
    '当当网',
    '北发图书网',
    'China-Pub',
    '卓越亚马逊',
    '新华书店.com',
    '99网上书城'
]
class Adebook(db.Model):
    ebid = db.IntegerProperty()
    source = db.IntegerProperty()
    title = db.StringProperty(multiline=False)
    author = db.StringProperty(multiline=False)
    descr = db.StringProperty(multiline=False)
    link = db.StringProperty(multiline=False)
    imgsrc = db.StringProperty(multiline=False)
    resv1 = db.StringProperty(multiline=False)
    resv2 = db.StringProperty(multiline=False)


#Return the Adebook DB Model
def readAdebookById(ebid):
    q = db.GqlQuery("SELECT * FROM Adebook WHERE ebid = :ebid", ebid=ebid)
    ebookModel = q.get()
    return ebookModel

def writeAdebook(ebookModel):
    ebookModel.put()

def listAllAdebooks(offset=0, limit=100):
    gplQuery = db.GqlQuery("SELECT * FROM Adebook ORDER BY ebid DESC")
    resultList = gplQuery.fetch(limit, offset)
    return resultList

#return a result map
def checkAdebook(source, title, author, descr, link, imgsrc):
    if source < 0 or source >= len(sourceInfo):
        return {'errMsg' : '无效的书店编号' + str(source)}
    if not title:
        return {'errMsg' : '电子书标题不能为空'}
    if len(title) > titleMaxLen:
        return {'errMsg' : '电子书标题太长，最多' + str(titleMaxLen) + '个字'}
    if not author:
        return {'errMsg' : '电子书作者名字不能为空'}
    if len(author) > authorMaxLen:
        return {'errMsg' : '电子书作者名字太长，最多' + str(authorMaxLen) + '个字'}
    if not descr:
        return {'errMsg' : '电子书简介不能为空'}
    if len(descr) > descrMaxLen:
        return {'errMsg' : '电子书简介太长，最多' + str(descrMaxLen) + '个字'}
    if not imgsrc:
        return {'errMsg' : '电子书图片地址url不能为空'}
    if len(imgsrc) > imgsrcMaxLen:
        return {'errMsg' : '电子书图片地址url太长，最大长度为' + str(imgsrcMaxLen)}
    if not link:
        return {'errMsg' : '电子书链接不能为空'}
    if len(link) > linkMaxLen:
        return {'errMsg' : '电子书链接太长，最大长度为' + str(linkMaxLen)}

    #apply to validators
    titleValidator = validator.PureTextValidator(title)
    filteredTitle = titleValidator.filter()
    authorValidator = validator.PureTextValidator(author)
    filteredAuthor = authorValidator.filter()
    descrValidator = validator.PureTextValidator(descr)
    filteredDescr = descrValidator.filter()
    
    imgsrcValidator = validator.LinkValidator(imgsrc)
    filteredImgsrc = imgsrcValidator.filter()
    if not filteredImgsrc:
        return {'errMsg' : '电子书图片地址url不符合规范，必需以http://开头'}
    linkValidator = validator.LinkValidator(link)
    filteredLink = linkValidator.filter()
    if not filteredLink:
        return {'errMsg' : '电子书链接不符合规范，必需以http://开头'}
    
    #return a filtered map
    filtered = {
        'source' : source,
        'title' : filteredTitle,
        'author' : filteredAuthor,
        'descr' : filteredDescr,
        'link' : filteredLink,
        'imgsrc' : filteredImgsrc
    }
    return filtered
